package utn.frba.matematicasuperior.main;

import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import utn.frba.matematicasuperior.controladores.EvaluarPolinomioControlador;
import utn.frba.matematicasuperior.controladores.GraficoControlador;
import utn.frba.matematicasuperior.controladores.PolinomioControlador;
import utn.frba.matematicasuperior.controladores.RootControlador;
import utn.frba.matematicasuperior.controladores.SoporteControlador;
import utn.frba.matematicasuperior.modelo.PolinomioInterpolante;
import utn.frba.matematicasuperior.utiles.Controlador;

public class Main extends Application {

	/************************************************************/
	/************************ ATRIBUTOS *************************/
	/************************************************************/

	private Stage escenario;
	private BorderPane rootLayout;
	private List<Controlador> controladores;

	/************************************************************/
	/*********************** PROPIEDADES ************************/
	/************************************************************/

	public List<Controlador> getControladores() {
		return controladores;
	}

	public void setControladores(List<Controlador> controladores) {
		this.controladores = controladores;
	}

	public Stage getEscenario() {
		return escenario;
	}

	public void setEscenario(Stage escenario) {
		this.escenario = escenario;
	}

	public BorderPane getRootLayout() {
		return rootLayout;
	}

	public void setRootLayout(BorderPane rootLayout) {
		this.rootLayout = rootLayout;
	}

	/************************************************************/
	/*********************** CONSTRUCTOR ************************/
	/************************************************************/

	public Main() {
		this.controladores = new ArrayList<>();
	}

	public static void main(String[] args) {
		launch(args);
	}

	/************************************************************/
	/******************** METODOS PUBLICOS **********************/
	/************************************************************/

	@Override
	public void start(Stage primaryStage) {
		try {
			this.escenario = primaryStage;
			this.escenario.setTitle("InterpolFX");
			this.escenario.getIcons().add(
					new Image("file:resources/images/icono.png"));

			// Load the root layout from the fxml file
			FXMLLoader loader = new FXMLLoader(
					Main.class.getResource("../vistas/RootLayout.fxml"));

			this.rootLayout = (BorderPane) loader.load();
			RootControlador controlador = loader.getController();
			this.getControladores().add(controlador);

			SplitPane centerPanel = (SplitPane) this.rootLayout.getCenter();

			AnchorPane panelSoporte = cargarSoporteLayout();
			AnchorPane panelPolinomio = cargarPolinomioLayout();

			centerPanel.getItems().set(0, panelSoporte);
			centerPanel.getItems().set(1, panelPolinomio);
			centerPanel.setDividerPosition(0, 0.295);

			setearMainControladores();

			Scene escena = new Scene(rootLayout);
			escena.setFill(Color.LIGHTGREY);
			this.escenario.initStyle(StageStyle.TRANSPARENT);

			primaryStage.setScene(escena);
			primaryStage.show();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setearMainControladores() {
		for (Controlador controller : this.getControladores()) {
			controller.setMain(this);
		}
	}

	private AnchorPane cargarSoporteLayout() {
		AnchorPane panelSoporte = null;
		try {
			FXMLLoader loader = new FXMLLoader(
					Main.class.getResource("../vistas/SoporteLayout.fxml"));
			panelSoporte = (AnchorPane) loader.load();

			SoporteControlador controlador = loader.getController();
			this.getControladores().add(controlador);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return panelSoporte;
	}

	private AnchorPane cargarPolinomioLayout() {

		AnchorPane panelPolinomio = null;
		try {
			FXMLLoader loader = new FXMLLoader(
					Main.class.getResource("../vistas/PolinomioLayout.fxml"));

			panelPolinomio = (AnchorPane) loader.load();
			PolinomioControlador controlador = loader.getController();
			this.getControladores().add(controlador);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return panelPolinomio;
	}

	public AnchorPane cargarEvaluarPolinomioLayout(
			PolinomioInterpolante polinomio) {
		AnchorPane panelEvaluarPolinomio = null;
		try {
			FXMLLoader loader = new FXMLLoader(
					Main.class
							.getResource("../vistas/EvaluarPolinomioLayout.fxml"));
			panelEvaluarPolinomio = (AnchorPane) loader.load();

			Stage dialogStage = new Stage();
			// dialogStage.initStyle(StageStyle.TRANSPARENT);
			dialogStage.initOwner(this.escenario);
			Scene scene = new Scene(panelEvaluarPolinomio);
			scene.setFill(Color.LIGHTGREY);
			dialogStage.setScene(scene);

			EvaluarPolinomioControlador controlador = loader.getController();
			controlador.setEscenario(dialogStage);
			controlador.setPolinomio(polinomio);
			this.getControladores().add(controlador);

			dialogStage.showAndWait();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return panelEvaluarPolinomio;
	}

	public AnchorPane cargarGraficoPolinomioLayout(
			PolinomioInterpolante polinomio) {
		AnchorPane panelGraficoPolinomio = null;
		try {
			FXMLLoader loader = new FXMLLoader(
					Main.class
							.getResource("../vistas/GraficoPolinomioLayout.fxml"));
			panelGraficoPolinomio = (AnchorPane) loader.load();

			Stage dialogStage = new Stage();
			dialogStage.initStyle(StageStyle.TRANSPARENT);
			dialogStage.initOwner(this.escenario);
			Scene scene = new Scene(panelGraficoPolinomio);
			scene.setFill(Color.LIGHTGREY);
			dialogStage.setScene(scene);

			GraficoControlador controlador = loader.getController();
			controlador.setPolinomio(polinomio);
			controlador.setEscenario(dialogStage);
			this.getControladores().add(controlador);

			dialogStage.showAndWait();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return panelGraficoPolinomio;
	}
}